package com.hnas.ecard.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnas.common.util.CommonUtils;
import com.hnas.common.web.BaseAjaxResponse;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.dto.DataBean;
import com.hnas.core.util.DateUtil;
import com.hnas.ecard.service.MerchantDetailService;
import com.hnas.ecard.service.dto.DTOMerchantDetail;
import com.hnas.ecard.service.search.DTOMerchantDetailSearch;

@Controller
@RequestMapping("/backend/merchantDetail")
public class MerchantDetailController {

	private static Logger log = LoggerFactory.getLogger(MerchantDetailController.class);

	@Autowired
	private MerchantDetailService merchantDetailService;
	
	@RequestMapping("search")
	public String search(ModelMap model,HttpServletRequest request,DTOMerchantDetailSearch con){
		try {
			DTOPagedList<DTOMerchantDetail> pg = merchantDetailService.page(con);
			model.put("dto", con);
			model.put("pg", pg);
		} catch (Exception e) {
			log.error("查询具体商家出现异常！");
			model.put("resultSize", 0);
			e.printStackTrace();
		}
		
		model.put("pageTitle", "具体商家管理");
		List<DataBean> beans = new ArrayList<DataBean>();
		beans.add(new DataBean("id", "id", "id", false, 125));
		beans.get(beans.size() - 1).setHidden(true);
		beans.add(new DataBean("merchantId", "merchantId", "所属商户", false, 125));
		beans.get(beans.size() - 1).setHidden(true);
		beans.add(new DataBean("name", "name", "名称", false, 125));
		beans.add(new DataBean("address", "address", "地址", true, 125));
		beans.add(new DataBean("sort", "sort", "排序", true, 125));
		beans.add(new DataBean("remark", "remark", "备注", true, 125));
		beans.add(new DataBean("createTime", "createTime", "创建时间", true, 125));
		beans.get(beans.size() - 1).setHidden(true);
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("isDeleted", "isDeleted", "是否删除", true, 125));
		beans.get(beans.size() - 1).setHidden(true);
		beans.get(beans.size() - 1).setDisplay(false);
		model.put("beans", beans);
		return "backend/ecard/listMerchantDetail";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public BaseAjaxResponse add(HttpServletRequest request, HttpServletResponse response, DTOMerchantDetail dto) {
		dto.setId(CommonUtils.getUUID());
		dto.setCreateTime(DateUtil.dateTimeToString(new Date()));
		dto.setIsDeleted(false);
		return save(request, response, dto);
	}

	@RequestMapping("/update")
	@ResponseBody
	public BaseAjaxResponse save(HttpServletRequest request, HttpServletResponse response, DTOMerchantDetail dto) {
		BaseAjaxResponse res = new BaseAjaxResponse();
		try {
			dto.setUpdateTime(DateUtil.dateTimeToString(new Date()));
			this.merchantDetailService.saveOrUpdate(dto);
			res.setResult(BaseAjaxResponse.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setErrorMessage(e.getMessage());
			res.setResult(BaseAjaxResponse.FAIL);
		}
		return res;
	}

	@RequestMapping("/show/{id}")
	@ResponseBody
	public DTOMerchantDetail show(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") String id) {
		return this.merchantDetailService.findById(id);
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public BaseAjaxResponse deleteById(String id){
		BaseAjaxResponse res = new BaseAjaxResponse();
		try {
			if(null==id){
				res.setErrorMessage("ID不能为空！");
				res.setResult(BaseAjaxResponse.FAIL);
				return res;
			}
			merchantDetailService.deleteById(id);
			return res.retrunSuccess(res);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("通过id查找商家分类异常");
			return res.retrunFail(res);
		}
	}
}
